package com.qfedu.user.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qfedu.user.po.Client;
import com.qfedu.user.po.User;
import com.qfedu.user.service.userService;
import com.qfedu.user.service.impl.userServiceimpl;
import com.sun.javafx.binding.StringFormatter;

import javafx.scene.control.Alert;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class userServlet
 */
@WebServlet("/userServlet")
public class userServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public userServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	    boolean boolResult = false;
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		userService userService = new userServiceimpl();
		PrintWriter writer = response.getWriter();
		String password=null;
		String idcard=null;
		String userName=null;
		User user1 = null;
		List<User> listUser = null;
		List<Client> listVip=null;
		System.out.println(method);
		switch (method) {
		case "Login":
			String user = request.getParameter("user");
			password = request.getParameter("password");
			boolResult = userService.doLogin(new Object[] { user, password });
			System.out.println(boolResult);
			writer.print(boolResult);
			writer.flush();
			break;
		case "Check":
			String checkIdCard=request.getParameter("idCard");
			break;
		case "deleteVip":
			String  deleteIdcard=request.getParameter("idCard");
			System.out.println(deleteIdcard);
			boolResult=userService.doVipDelete(new Object [] {deleteIdcard});
			writer.print(boolResult);
			writer.flush();
			break;
		case "VIPregister":
			String CLIENT_NAME=request.getParameter("userName");
			String PHONE_NUM=request.getParameter("phonenumber");
			String IDCARD=request.getParameter("idCard");
			String BIRTHDAY=request.getParameter("birthday");
			boolResult=userService.doVipregister(new Object [] {CLIENT_NAME,PHONE_NUM,IDCARD,BIRTHDAY});
			break;
		case "updateVip":
			String userName1=request.getParameter("userName");
			String phoneNumber=request.getParameter("phoneNumber");
			String integration=request.getParameter("integration");
			String idcard1=request.getParameter("idCard");
			boolResult=userService.doVipUpdate(new Object [] {userName1,phoneNumber,integration,idcard1});
			response.sendRedirect("selectVip.jsp");
			break;
		case "selectVip":
			String phone_number=request.getParameter("phoneNumber");
			idcard=request.getParameter("idCard");
			listVip=userService.doVipselect(new Object [] {phone_number,idcard});
			request.setAttribute("listVip",listVip);
			request.getRequestDispatcher("listVip.jsp").forward(request, response);
			break;
		case "selectVipOne":
			idcard=request.getParameter("idCard");
			listVip=userService.selectOneById(new Object [] {idcard});
			request.setAttribute("listVip", listVip.get(0));
			request.getRequestDispatcher("updateVip.jsp").forward(request, response);
			break;
//		case "selectUserOne":
//			//通过id查询User信息
//			String userId=request.getParameter("userId");
//			listUser=userService.selectOneById1(userId);
//			request.setAttribute("listUser", listUser.get(0));
//			request.getRequestDispatcher("updateUser.jsp").forward(request, response);
//			
//			break;
		case "selectUserOne1":
			//通过id查询User信息
			int userId1=Integer.parseInt(request.getParameter("userId"));
			String name2=request.getParameter("name");
			listUser=userService.selectOneById1(new Object[] {userId1,name2});
			System.out.println(listUser);
			request.setAttribute("listUser", listUser);
			request.getRequestDispatcher("userList.jsp").forward(request, response);
			break;	
		case "selectUserOne":
//			//通过id查询User信息
			int id=Integer.parseInt(request.getParameter("id"));
			user1=userService.selectOneById3(new Object[] {id});
			System.out.println(user1);
			request.setAttribute("user1", user1);
			request.getRequestDispatcher("updateUser.jsp").forward(request, response);
			break;	
			/**
			 * 查询所有User信息
			 */
		case "selectUserAll":
			
			List<User> listUser1 = userService.selectAll();
			System.out.println(listUser1);
			request.setAttribute("listUser1",listUser1);
			request.getRequestDispatcher("user_emplist.jsp").forward(request, response);
			break;
			/**
			 * 更新User信息
			 */
		case "updateUser":
			String name = request.getParameter("name");
			String password1= request.getParameter("password");
			String userPosition = request.getParameter("userPosition");
			userName = request.getParameter("userName");
			String userSex = request.getParameter("userSex");
			String userHireDate = request.getParameter("userHireDate");
			String userLeaveDate = request.getParameter("userLeaveDate");
			String userIdCard = request.getParameter("userIdCard");
			String userAge = request.getParameter("userAge");
		    //User  user = new User(name,password1,userPosition, userName,userSex, userHireDate, userLeaveDate,userIdCard, userAge);
			// 执行方法，更新数据库中的数据
			//User对象
			//boolResult = userService.userUpdate(user);
			// 向页面输出返回结果
			if (boolResult) {
				writer.println("成功");
			}else {
				writer.println("失败");
			}
			
			break;
		case "RegisterUser":
			password= request.getParameter("password");
			String userPosition1 = request.getParameter("userPosition");
			userName = request.getParameter("userName");
			String userSex1 = request.getParameter("userSex");
		    String name1 = request.getParameter("name");
			String userIdCard1 = request.getParameter("userIdCard");
			String userAge1 = request.getParameter("userAge");
			boolResult=userService.doUserRegister(new Object [] {userName,password,userPosition1,name1,userSex1,userIdCard1,userAge1});
			
			break;
		case "userCheck":
			String userIdCard2 = request.getParameter("userIdCard");
			boolResult=userService.userCheck(userIdCard2);
			break;
		case "deleteUserOne":
			int userId2 = Integer.parseInt(request.getParameter("userId"));
			boolResult=userService.doUserDelete(new Object [] {userId2});
			break;
		default:
			break;
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
